package com.aaa.dem1104.d02;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: whj
 * @Version 1.0
 * @Date: 2022/11/4  19:28
 */
public class Test2 {
    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("何雪",21,"中国",'F'));
        personList.add(new Person("李康",22,"中国",'M'));
        personList.add(new Person("汪豪杰",18,"中国",'F'));
        personList.add(new Person("汪豪杰帅",24,"美国",'M'));
        personList.add(new Person("汪豪杰真帅",22,"英国",'F'));
        personList.add(new Person("汪豪杰真是帅",20,"中国",'M'));
        personList.add(new Person("汪豪杰真的是帅",22,"中国",'M'));
        personList.add(new Person("汪豪杰真的是巨帅",20,"中国",'F'));
        //1）找到年龄大于18岁的人并输出；
        // filter 它就是中间操作的方法。如果没有执行终止操作方法那么中间操作都不会执行，当执行了终止操作方法时才会执行中间操作的方法
        personList.stream().filter(item->{
            return item.getAge()>18;
        }).forEach(item-> System.out.println(item));
        //forEach属于终止操作方法。
        // 2）找出所有中国人的数量。
        long count = personList.stream().filter(item -> item.getCountry().equals("中国")).count();
        System.out.println(count);
    }
}

class Person {
    private String name;
    private Integer age;
    private String country;
    private char sex;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", country='" + country + '\'' +
                ", sex=" + sex +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public Person(String name, Integer age, String country, char sex) {
        this.name = name;
        this.age = age;
        this.country = country;
        this.sex = sex;
    }
}
